package com.demo.core.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.base.model.InvitationCode;
import com.demo.base.util.DateTimeUtil;
import com.demo.base.util.StringUtil;
import com.demo.core.dao.InvitationCodeDao;

@Transactional
@Service
public class InvitationCodeService {

	@Autowired
	private InvitationCodeDao invitationCodeDao;
	
	/**
	 * 自动生成邀请码
	 * @param num
	 * @param type
	 */
	public void automaticGeneration(Integer num,String type){
		
		for(int i = 0 ; i< num ; i++){
			InvitationCode invitationCode = new InvitationCode();
			invitationCode.setRecordCreateTime(DateTimeUtil.getNowDateStr(DateTimeUtil.yyyy_MM_dd_HH_mm_ss));
			invitationCode.setCode(StringUtil.createRandom(true, 8));
			invitationCode.setEnable("0");
			invitationCode.setType(type);
			invitationCodeDao.add(invitationCode);
		}
	}
	
	/**
	 * 验证邀请码是否可用
	 * @param code
	 * @return
	 */
	public boolean validateInvitationCode(String code){
		
		return  invitationCodeDao.validateInvitationCode(code);
	}
	
	/**
	 * 根据邀请码获取邀请码实体
	 * @param code
	 * @return
	 */
	public InvitationCode getInvitationCodeByCode(String code){
		
		return invitationCodeDao.getInvitationCodeBycode(code);
	}
	
	/**
	 * 修改邀请码
	 * @param invitationCode
	 */
	public void updateInvitationCode(InvitationCode invitationCode){
		
		invitationCodeDao.update(invitationCode);
	}
}
